<template>
  <div id="app" class="skLifeStudentTable">
    <a-table :loading="loading" :pagination="false" :scroll="{x:tebleXScroll,y:500}" :columns="columns" :data-source="dataSource" :rowKey="rowKey" bordered >
    </a-table>
  </div>
</template>
<script>
import { drinkAndToilet } from "@/api/health.js";
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil';
export default {
  name: 'skLifeStudentTable',
  data() {
    return {
      loading:false,
      columns:[
      ],
      dataSource:[],
      dateType:"week",
      rowKey:""
    }
  },
  props:["detailName"],
  components: {},

  filters: {},

  computed: {
    tebleXScroll:{
      get(){
        return this.$store.state.skCommon.columnsWidth
      },
      set(val){
        this.$store.dispatch("setcolumnsWidth",val)
      }
    }
  },
  
  created() {
    // this.getStudentData();
  },
  
  activated() {},
  
  mounted() {},
  
  methods: {
    // 获取学生具体信息
    getStudentData(dataForm){
      this.dateType = dataForm.dateType;
      console.log(dataForm);
      this.handleData(dataForm)
    },
    handleData(dataForm){
      let params = {
        dateType: dataForm.dateType=="historySem"?"sem":dataForm.dateType,
        queryDate: this.$skCommon.getDate(0),
        classId: dataForm.classId == '0' ? null : dataForm.classId,
        semesterId:dataForm.semesterId == "历史学期" ?dataForm.semesterId2:dataForm.semesterId,
      }
      let url = "";
      if (this.detailName == '喝水') {
        url = '/health/drink/classInfoList'
      }
      if (this.detailName == '如厕') {
        url = '/health/toilet/classInfoList'
      }
      if (this.detailName == '睡眠') {
        url = '/health/sleep/selectStudentInfo'
      }
      this.columns = [];
      drinkAndToilet(url,params).then((res)=>{
        if(res.success){
          let dataColumns = [],dateArr = [],newDate = [];
          if (this.detailName == "睡眠") {
            res.result.forEach(item=>{
              if (item.detail.length > 0) {
                let json = {
                  name:item.name,
                  average:item.average.toFixed(2),
                  sleepCount :item.sleepCount,
                  id:item.id
                };
                item.detail.forEach((j,i) => {
                  dateArr.push(j.date)
                  json["average"+j.date.substring(5,11)] = j.average.toFixed(2);
                  json["sleepCount"+j.date.substring(5,11)] = j.sleepCount.toFixed(2);
                })
                dataColumns.push(json)
              }
            })
            newDate = [...new Set(dateArr)]
          }
          if (this.detailName == "喝水" || this.detailName == "如厕") {
            res.result.forEach(item=>{
              if (item.detail.length > 0) {
                let json = {}
                json = {
                  name:item.stuName,
                  average:item.avg.toFixed(2),
                  count:item.count.toFixed(2),
                  type:item.classType,
                  id:item.id
                };
                item.detail.forEach((j,i) => {
                  dateArr.push(j.startDate)
                  // json["average"+j.startDate.substring(5,11)] = j.avg.toFixed(2);
                  let dateString;
                  if (j.startDate) dateString = j.startDate.substring(5,11)
                  json["count"+dateString] = j.count.toFixed(2);
                })
                dataColumns.push(json)
              }
            })
            newDate = [...new Set(dateArr)]
          }
          this.dataSource = dataColumns;
          this.setColumns(newDate);
        } else {
          this.$message.warning(res.message);
        }
      }).catch((error)=>{
        console.error(error);
      })

    },
    // 表头
    setColumns(dateArr){
      if (this.detailName == "喝水" || this.detailName == "如厕") {
        this.stuId = "stuId"
        // this.columns.splice(1,0,{
        //   title: '年级',
        //   align: 'center',
        //   dataIndex: 'classType',
        //   customRender: function(text) {
        //     return filterDictTextByCache('class_type', text)
        //   }
        // },
        // {
        //   title: '班级',
        //   align: 'center',
        //   dataIndex: 'className'
        // })
        this.columns.push(
          {
            title: '姓名',
            align: 'center',
            fixed: 'left',
            width:100,
            dataIndex: 'name',
          },
          
          {
            title: `${this.detailName}总次数`,
            align: 'center',
            dataIndex: 'count'
          },
          {
            title:  `平均${this.detailName}次数`,
            align: 'center',
            dataIndex: 'average'
          },
        )
        dateArr.sort().forEach((item,i) => {
          if (item) {
            let date = "";
            if (this.dateType == "week" || this.dateType == "month") {
              date = item.substring(5,11)
            }else{
              date = item.substring(5,8)
            }
            let tableHeaderJson = {
              dateType:this.dateType,
              startDate:item,
            }
            let json = {
              title: this.$skCommon.tableHeaderDate(tableHeaderJson),
              align: 'center',
              dataIndex: `count${date}`,
              customRender: function(text) {
                  return text || '--'
                }
            }
            this.columns.push(json)
          }
        });
      }
      if (this.detailName == "睡眠") {
        this.rowKey = "id";
        this.columns = [
          {
            title: '姓名',
            align: 'center',
            fixed: 'left',
            width: 100,
            dataIndex: 'name'
          },
          {
            title: '平均睡眠时长(分钟)',
            align: 'center',
            width: 180,
            dataIndex: 'average'
          },
        ]
        let _this = this;
        dateArr.sort().forEach(item => {
          let date = "";
          let detailName = _this.detailName
          if (this.dateType == "week" || this.dateType == "month") {
            date = item.substring(5,11)
          }else{
            date = item.substring(5,8)
            detailName = '平均' + detailName
          }
          let json = {}
          let tableHeaderJson = {
            dateType:this.dateType,
            startDate:item
          }
          json = {
            title:this.$skCommon.tableHeaderDate(tableHeaderJson),
            align: 'center',
            children: [
              {
                title: `${detailName}时长(分钟)`,
                align: 'center',
                dataIndex: `average${date}`,
                customRender: function(text) {
                  return text || '--'
                }
              },
              // {
              //   title: `总${_this.detailName}时长(分钟)`,
              //   align: 'center',
              //   dataIndex: `sleepCount${date}`,
              //   customRender: function(text) {
              //     return text || '--'
              //   }
              // }
            ]
          }
          this.columns.push(json)
        });
      }
      this.tebleXScroll = this.columns.length
    },
  }
}
</script>
<style scoped>
/* @import url(); 引入css类 */
</style>
